import { NodeType } from "/enum/NodeTypeEnum";
import { Graphic } from "/graphic/type";
import { mixins } from "/tool";

export interface NodeConfig {
    // allow any custom attribute
    [index: string]: any;
    x?: number;
    y?: number;
    width?: number;
    height?: number;
    visible?: boolean;
    listening?: boolean;
    id?: string;
    name?: string;
    opacity?: number;
    // scale?: Vector2d;
    scaleX?: number;
    scaleY?: number;
    rotation?: number;
    rotationDeg?: number;
    // offset?: Vector2d;
    offsetX?: number;
    offsetY?: number;
    draggable?: boolean;
    dragDistance?: number;
    // dragBoundFunc?: (this: Node, pos: Vector2d) => Vector2d;
    preventDefault?: boolean;
    type?:NodeType
    // globalCompositeOperation?: globalCompositeOperationType;
    // filters?: Array<Filter>;
  }
export default class Node<T=any,U=any>{
    [key: string]: any;
    x?: number;
    y?: number;
    width?: number;
    height?: number;
    visible?: boolean;
    listening?: boolean;
    id?: string;
    name?: string;
    opacity?: number;
    // scale?: Vector2d;
    scaleX?: number;
    scaleY?: number;
    rotation?: number;
    rotationDeg?: number;
    // offset?: Vector2d;
    offsetX?: number;
    offsetY?: number;
    draggable?: boolean;
    dragDistance?: number;
    // dragBoundFunc?: (this: Node, pos: Vector2d) => Vector2d;
    preventDefault?: boolean;
    type:NodeType
    parent:U
    children:T[]=[]
    root:U;
    constructor(config?:NodeConfig){
        this.setAttrs(config);
    }
    setAttrs(config:NodeConfig){
        if(typeof config !== "object"){
            return;    
        }
        Object.keys(config).forEach(key=>{
            if(typeof config[key] === "number"){
                this[key] += config[key]
            }else{
                this[key] = config[key]
            }
        })
    }
    setParent(parent:any){
        this.parent = parent;
    }
    setRoot(node:any){
        this.root =node;
    }
    setChildren(children:T){
        this.children.push(children);
    }
    preDraw(ctx:CanvasRenderingContext2D){
		const {drawImg,drawX,drawY,drawOffsetX,drawOffsetY} = this.config;
		if(drawImg){
			ctx.drawImage(drawImg,drawX,drawY,drawOffsetX,drawOffsetY);
		}
	}
}
